// 
//  Author:
//    Andy Selvig ajselvig@gmail.com
// 
//  Copyright (c) 2010, Andy Selvig
// 
//  All rights reserved.
// 
//  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
// 
//     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in
//       the documentation and/or other materials provided with the distribution.
//     * Neither the name of the [ORGANIZATION] nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
// 
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
//  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
//  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
//  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
//  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
//  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
//  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 

using System;
using System.Collections.Generic;
using System.Reflection;
using System.IO;

namespace Shake
{
	/// <summary>
	/// Collects all the tasks in one or more assemblies.
	/// </summary>
	public class TaskManager
	{
		public TaskManager()
		{
			TaskGroups = new List<TaskGroup>();
			Output = new StringWriter();
		}

		private TextWriter _output;
		/// <summary>
		/// This writer gets assigned to all task groups that this manager finds.
		/// </summary>
		public TextWriter Output { 
			get {return _output;}
			set {
				_output = value;
				foreach (var group in TaskGroups)
					group.Output = value;
			}
		}

        /// <summary>
        /// Whether or not to print verbose outputs during execution.
        /// </summary>
        public bool Verbose { get; set; }
		
		/// <summary>
		/// All task groups that are being managed (accessed by namespace).
		/// </summary>
		public List<TaskGroup> TaskGroups { get; private set; }
		
		/// <summary>
		/// Loads the assembly and extracts all tasks from it.
		/// </summary>
		public void LoadAssembly(Assembly asm)
		{
			if (Output == null)
				throw new Exception("Need to have a valid TaskManager Output before you can parse assemblies.");

			var types = asm.GetExportedTypes();
			foreach (var type in types) {
				if (type.IsSubclassOf(typeof(TaskGroup))) {
					var group = Activator.CreateInstance(type) as TaskGroup;
					group.Output = Output;
					TaskGroups.Add(group);
				}
			}
		}
		
		/// <summary>
		/// Finds a task by name.
		/// </summary>
		public TaskAttribute FindTask(string name)
		{
			// determine the namespace and task name
			var comps = name.Split(':');
			if (comps.Length > 2)
				throw new Exception("tasks name must be prepended with a namespace.");
			var taskName = comps[comps.Length - 1];
			var ns = name.Substring(0, name.Length - name.LastIndexOf(':') - 2);
			
			// look for the right group
			foreach (var group in TaskGroups) {
				if (group.Namespace == ns) {
					var task = group.GetTask(taskName);
					if (task != null)
						return task;
				}
			}
			return null;
		}

		/// <summary>
		/// Lists all tasks available in the manager.
		/// </summary>
		public IList<TaskAttribute> AllTasks
		{
			get
			{
				var tasks = new List<TaskAttribute>();
				foreach (var group in TaskGroups)
				{
					tasks.AddRange(group.Tasks);
				}
				return tasks;
			}
		}
		
	}
}

